package com.fang.web;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fang.entity.Cart;
import com.fang.entity.Orders;
import com.fang.entity.OrdersItem;
import com.fang.entity.User;
import com.fang.service.CartService;
import com.fang.service.OrdersService;
import com.fang.utils.UUIDUtils;

public class OrdersServlet extends BaseServlet {
//	支付成功，收到支付平台通知，修改状态
	public void paysuccess(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//判断用户是否登录
				User user = (User) req.getSession().getAttribute("loginuser");
				if(user==null){
					resp.sendRedirect(req.getContextPath()+"/login.jsp");
					return;
				}
				
				int uid = user.getId();
				String id = req.getParameter("id");
				OrdersService service = new OrdersService();
				 service.paysuccess(uid,id);
				resp.sendRedirect(req.getContextPath()+"/orders?method=query");
	}
	
	//去支付，跳转到详情页面
	public void gopay(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//判断用户是否登录
		User user = (User) req.getSession().getAttribute("loginuser");
		if(user==null){
			resp.sendRedirect(req.getContextPath()+"/login.jsp");
			return;
		}
		
		int uid = user.getId();
		String id = req.getParameter("id");
		OrdersService service = new OrdersService();
		Orders orders= service.queryorders(id,uid);
		req.setAttribute("orders", orders);
		List <OrdersItem> list = service.queryitem(id);
		req.setAttribute("list", list);
		
		req.getRequestDispatcher("/orders_detail.jsp").forward(req, resp);
	}
	
	//取消订单，修改状态
	public void cansel(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//判断用户是否登录
		User user = (User) req.getSession().getAttribute("loginuser");
		if(user==null){
			resp.sendRedirect(req.getContextPath()+"/login.jsp");
			return;
		}
		
		int uid = user.getId();
		String id = req.getParameter("id");
		OrdersService service = new OrdersService();
		 service.cansel(uid,id);
		resp.sendRedirect(req.getContextPath()+"/orders?method=query");
		
	}
	
	public void newadd(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//判断用户是否登录
		User user = (User) req.getSession().getAttribute("loginuser");
		if(user==null){
			resp.sendRedirect(req.getContextPath()+"/login.jsp");
			return;
		}
		
		int uid = user.getId();
		String id = req.getParameter("id");
		OrdersService service = new OrdersService();
		 service.newadd(uid,id);
		resp.sendRedirect(req.getContextPath()+"/orders?method=query");
		
	}
	
	public void query(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//判断用户是否登录
		User user = (User) req.getSession().getAttribute("loginuser");
		if(user==null){
			resp.sendRedirect(req.getContextPath()+"/login.jsp");
			return;
		}
		
		int uid = user.getId();
		OrdersService service = new OrdersService();
		 List<Orders> olist = service.query(uid);
		 req.setAttribute("olist", olist);
		 req.getRequestDispatcher("/orders.jsp").forward(req, resp);
		
	}
	
	
	public void submit(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//判断用户是否登录
		User user = (User) req.getSession().getAttribute("loginuser");
		if(user==null){
			resp.sendRedirect(req.getContextPath()+"/login.jsp");
			return;
		}
		//2. 封装订单数据
		//订单编号
		String id = UUIDUtils.getUUID();
		Integer uid = user.getId();
		// 计算总金额，数据来自购物车
		CartService cservice = new CartService();
		List<Cart> clist = cservice.querybyuid(uid);
		Double total = 0d;
		for (int i = 0; i < clist.size(); i++) {
			double estoreprice = clist.get(i).getGoods().getEstoreprice();
			double buynum = clist.get(i).getBuynum();
			total = total + estoreprice*buynum;
		}
		//获取地址
		String pp = req.getParameter("pp");
		String cc = req.getParameter("cc");
		String dd = req.getParameter("dd");
		String dAddress = req.getParameter("dAddress");
		String zipcode = req.getParameter("zipcode");
		String name = req.getParameter("name");
		String telephone = req.getParameter("telephone");
		String address=pp+cc+dd+"  "+dAddress+"  邮编 : "+zipcode+"  收货人姓名：   " +name + "  电话:    "+telephone;
		Integer status = 1;
		Date time = new Date();
		Orders orders = new Orders(id,uid, total,address,status,time);
		
		//3. 封装订单明细数据:就是购物车中的所有商品
		List<OrdersItem>   olist = new ArrayList<OrdersItem>();
		for (Cart cart: clist) {
			olist.add(new OrdersItem(id, cart.getGid(), cart.getBuynum()));
		}
		
		//4. 将数据交给业务层
		OrdersService service = new OrdersService();
		service.submit(orders,olist);
		
		//5. 重定向到查询订单的页面
		resp.sendRedirect(req.getContextPath() + "/orders?method=query");
	}

	public void load(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//判断用户是否登录
		User user = (User) req.getSession().getAttribute("loginuser");
		if(user==null){
			resp.sendRedirect(req.getContextPath()+"/login.jsp");
			return;
		}
		String pid = req.getParameter("pid");
		OrdersService service = new OrdersService();
		String json = service.queryPCD(pid);
		
		resp.getWriter().write(json);
	}

}